package cn.dakaqi.entities;

import cn.dakaqi.entities.user.Volunteer;
import cn.dakaqi.utils.qrcode.ImageType;
import cn.dakaqi.utils.qrcode.QRCode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.ByteArrayOutputStream;
import java.util.Date;

/**
 * Created by chunyang on 2016/3/21.
 */

@Entity
@Table(name = "U_GROUP")
@Slf4j
@ToString
public class Group extends IdEntity
{

    private static final long serialVersionUID = 5601602554411627932L;
    private String name;
    private String shortName;
    private String groupCode;
    private String logo;
    private long times;//公益时间
    private int volunteers;//成员量
    private int actvitys;//活动量
    private int liveness;//活跃度
    private String demo;//简介
    private String serviceField;//服务领域
    private String setupDate;//成立日期
    private Date createTime;//入驻日期
    private String regAddress;//注册地
    private String addressCode;//地址编码
    private String monitor;//负责人
    private int goodComment;//好评
    private int mediumComment;//中评
    private int badComment;//差评
    private String province;
    private String city;
    private String district;
    private String phone;
    private int delStatus;
    private Volunteer createUser;
    private String code;//机构代码
    private String codeImg;//机构代码图
    private String platform;//挂靠到哪些平台
    private String client;
    private long oldID;

    public String getAddressCode()
    {
        return addressCode;
    }

    public void setAddressCode(String addressCode)
    {
        this.addressCode = addressCode;
    }

    public String getClient()
    {
        return client;
    }

    public void setClient(String client)
    {
        this.client = client;
    }

    public String getPlatform()
    {
        return platform;
    }

    public void setPlatform(String platform)
    {
        this.platform = platform;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getCodeImg()
    {
        return codeImg;
    }

    public void setCodeImg(String codeImg)
    {
        this.codeImg = codeImg;
    }

    public String getShortName()
    {
        return shortName;
    }

    public void setShortName(String shortName)
    {
        this.shortName = shortName;
    }

    public long getOldID()
    {
        return oldID;
    }

    public void setOldID(long oldID)
    {
        this.oldID = oldID;
    }

    public String getGroupCode()
    {
        return groupCode;
    }

    public void setGroupCode(String groupCode)
    {
        this.groupCode = groupCode;
    }

    public String getLogo()
    {
        return logo;
    }

    public void setLogo(String logo)
    {
        this.logo = logo;
    }

    public long getTimes()
    {
        return times;
    }

    public void setTimes(long times)
    {
        this.times = times;
    }


    public int getVolunteers()
    {
        return volunteers;
    }

    public void setVolunteers(int volunteers)
    {
        this.volunteers = volunteers;
    }

    public int getActvitys()
    {
        return actvitys;
    }

    public void setActvitys(int actvitys)
    {
        this.actvitys = actvitys;
    }

    public int getLiveness()
    {
        return liveness;
    }

    public void setLiveness(int liveness)
    {
        this.liveness = liveness;
    }

    public String getDemo()
    {
        return demo;
    }

    public void setDemo(String demo)
    {
        this.demo = demo;
    }

    public String getServiceField()
    {
        return serviceField;
    }

    public void setServiceField(String serviceField)
    {
        this.serviceField = serviceField;
    }

    public String getSetupDate()
    {
        return setupDate;
    }

    public void setSetupDate(String setupDate)
    {
        this.setupDate = setupDate;
    }
    // 设定JSON序列化时的日期格式
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getRegAddress()
    {
        return regAddress;
    }

    public void setRegAddress(String regAddress)
    {
        this.regAddress = regAddress;
    }

    public String getMonitor()
    {
        return monitor;
    }

    public void setMonitor(String monitor)
    {
        this.monitor = monitor;
    }

    public int getGoodComment()
    {
        return goodComment;
    }

    public void setGoodComment(int goodComment)
    {
        this.goodComment = goodComment;
    }

    public int getMediumComment()
    {
        return mediumComment;
    }

    public void setMediumComment(int mediumComment)
    {
        this.mediumComment = mediumComment;
    }

    public int getBadComment()
    {
        return badComment;
    }

    public void setBadComment(int badComment)
    {
        this.badComment = badComment;
    }

    public String getProvince()
    {
        return province;
    }

    public void setProvince(String province)
    {
        this.province = province;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getDistrict()
    {
        return district;
    }

    public void setDistrict(String district)
    {
        this.district = district;
    }

    public String getPhone()
    {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public int getDelStatus()
    {
        return delStatus;
    }

    public void setDelStatus(int delStatus)
    {
        this.delStatus = delStatus;
    }



    public Group()
    {
    }

    // JPA 基于USER_ID列的多对一关系定义
    @ManyToOne
    @JoinColumn(name = "VOLUNTEER_ID")
    @JsonIgnore
    public Volunteer getCreateUser()
    {
        return createUser;
    }

    public void setCreateUser(Volunteer createUser)
    {
        this.createUser = createUser;
    }

    public Group(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    //    @Override
//    public String toString() {
//        return ToStringBuilder.reflectionToString(this);
//    }

    public static String getQr(String groupCode)
    {
        String qr = "";
        if(StringUtils.isNotBlank(groupCode))
        {
            ByteArrayOutputStream out = QRCode.from(groupCode).to(ImageType.PNG).stream();
            byte[] data = out.toByteArray();
            qr = new String(Base64.encodeBase64(data));
            log.info(qr);
        }
        return qr;
    }

}
